package com.example.t410.controller;

import com.example.t410.dao.NootBook;
import com.example.t410.dao.NootbookResult;
import com.example.t410.service.NootBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//   path:/pages/MyMemo
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/pages/MyMemo")
public class nootBookController {
    @Autowired
     NootBookService nootBookService;
//    查询
    @GetMapping("{userId}")
    public NootbookResult selectUser(@PathVariable String userId){
        List<NootBook> data= nootBookService.select(userId);
        log.info("查询用户id为：{}",userId);
        return NootbookResult.Success(data);
    }
//    根据MemoId和uerrId回显
    @GetMapping
    public NootbookResult selectMenoId(@RequestParam("userId") String userId,
                                     @RequestParam("MemoId") Integer MemoId){
        NootBook data= nootBookService.selectId(userId,MemoId);
        log.info("显示用户：{}的第{}条记录",userId,MemoId);
        return NootbookResult.Success(data);
    }
//    增加
    @PostMapping("/add")
    public NootbookResult insertText(@RequestBody NootBook nootBook){
        nootBookService.insertText(nootBook);
        log.info("id为：{},的用户添加笔记内容为：{}",nootBook.getUserId(),nootBook.getMemotext());
        return NootbookResult.Success();
    }
//    删除
    @DeleteMapping ("/delect")
    public NootbookResult delectId(@RequestParam String userId,@RequestParam Integer MemoId){
        nootBookService.delect(userId,MemoId);
        log.info("成功删除用户：{},的第{}条数据",userId,MemoId);
        return NootbookResult.Success();
    }
//    修改
    @PostMapping
    public NootbookResult update(@RequestBody NootBook nootBook){
        nootBookService.update(nootBook);
        log.info("用户{}，更新数据成功",nootBook.getUserId());
        return NootbookResult.Success();
    }

}
